package com.example.service.ServiceImpl;

import com.example.entity.Venue;
import com.example.entity.VenueOrder;
import com.example.mapper.VenueMapper;
import com.example.service.VenueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class VenueServiceImpl implements VenueService {

    @Resource
    VenueMapper venueMapper;

    //查询全部场地
    @Override
    public PageInfo<Venue> queryAllVenue(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Venue> venues = venueMapper.queryAllVenue();
        PageInfo<Venue> pageInfo = new PageInfo<>(venues);
        return pageInfo;
    }

    //通过ID查询场地
    public Venue queryVenueById(int venueId){
        return venueMapper.queryVenueById(venueId);
    }

    //通过名字模糊查询场地
    @Override
    public PageInfo<Venue> queryVenueByName(Integer pageNum,Integer pageSize,String venueName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Venue> venues = venueMapper.queryVenueByName(venueName);
        PageInfo<Venue>pageInfo = new PageInfo<>(venues);
        return pageInfo;
    }

    //添加场地
    @Override
    public int addVenue(Venue venue) {
        return venueMapper.addVenue(venue);
    }

    //删除场地
    @Override
    public int deleteVenue(int venueId) {
        return venueMapper.deleteVenue(venueId);
    }

    //修改场地
    @Override
    public int updateVenue(Venue venue) {
        return venueMapper.updateVenue(venue);
    }

    //更改场地状态
    @Override
    public int updateVenueState(int venueId, int state) {
        return venueMapper.updateVenueState(venueId,state);
    }

    //查询场地费用
    @Override
    public String venueCharges(int venueId) {
        return venueMapper.venueCharges(venueId);
    }


    @Override
    public String announcement() {
        return null;
    }

    //查询场地一周信息
    @Override
    public String weeklyInfo(int venueId) {
        return venueMapper.weeklyInfo(venueId);
    }

    //预定场地，保存订单信息
    public int venueBook(String startTime, String endTime, int venueId) throws ParseException {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date start = simple.parse(startTime);
        Date end = simple.parse(endTime);
        long from = start.getTime();
        long to = end.getTime();
        int countTime = (int) ((to-from)/(1000*60));
        int charges = venueMapper.queryVenueById(venueId).getCharges();
        int countCharges = countTime*charges;
        VenueOrder venueOrder = new VenueOrder(1,start,end,countTime,charges,countCharges,venueId);
        return venueMapper.venueBook(venueOrder);
    };
}
